package com.bbyb.operating.examination.controller;

import com.bbyb.operating.examination.model.po.Role;
import com.bbyb.operating.examination.model.vo.CommonResult;
import com.bbyb.operating.examination.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 添加角色
     * @param role
     * @return
     */
    @ApiOperation("添加角色")
    @PostMapping(value = "add")
    public CommonResult<Boolean> addUser(@Valid @RequestBody Role role){
        if(role==null){
            return new CommonResult<>(601,"用户信息不能为空");
        }

        if(StringUtils.isBlank(role.getRoleCode())){
            return new CommonResult<>(601,"用户编码不能为空");
        }

        if(StringUtils.isBlank(role.getRoleName())){
            return new CommonResult<>(601,"用户名不能为空");
        }

        String errorMsg=roleService.addRole(role);
        if(StringUtils.isNotBlank(errorMsg)){
            return new CommonResult<>(601,errorMsg);
        }
        return new CommonResult<>(true);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @ApiOperation("删除角色")
    @PostMapping("/deleteByPrimaryKey")
    public CommonResult<Boolean> deleteByPrimaryKey(@RequestParam Integer id){
        String errorMsg=roleService.deleteByPrimaryKey(id);
        if(StringUtils.isNotBlank(errorMsg)){
            return new CommonResult<>(601,errorMsg);
        }
        return new CommonResult<>(true);
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @ApiOperation("修改角色")
    @PostMapping("/updateByPrimaryKey")
    public CommonResult<Boolean> updateByPrimaryKey(@Valid @RequestBody Role role){
        if(role==null){
            return new CommonResult<>(601,"用户信息不能为空");
        }
        if(role.getId()==null){
            return new CommonResult<>(601,"用户id不能为空");
        }
        String errorMsg=roleService.updateByPrimaryKey(role);
        if(StringUtils.isNotBlank(errorMsg)){
            return new CommonResult<>(601,errorMsg);
        }
        return new CommonResult<>(true);
    }

    /**
     * 查询所有
     * @return
     */
    @ApiOperation("查询所有")
    @PostMapping("/selectAll")
    public CommonResult<List<Role>> selectAll(){
        return new CommonResult<>(roleService.selectAll());
    }

    /**
     * 查询id
     * @param id
     * @return
     */
    @ApiOperation("查询id")
    @GetMapping("/selectByPrimaryKey")
    public CommonResult<Role> selectByPrimaryKey(@RequestParam("id") Integer id){
        return new CommonResult<>(roleService.selectByPrimaryKey(id));
    }
}
